package com.huo.filestransport.controller;

import java.util.ArrayList;
import java.util.List;

import com.huo.common.enumtype.FileTypeEnum;
import com.huo.common.enumtype.R;
import com.huo.filestransport.service.FtpService;
import com.huo.filestransport.vo.FtpFileSimpleInfo;
import com.huo.filestransport.vo.FtpServerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huo.filestransport.service.SftpService;

@Controller
@RequestMapping("/sftp")
public class TransportSftpController {

	@Autowired
	private SftpService sftpService;
	
	@Autowired
	private FtpService ftpService;

	@Autowired
	private FtpServerInfo ftpServerInfo;

	@ResponseBody
	@RequestMapping("")
	public R aliveServer() {
		return R.ok();
	}

	@ResponseBody
	@RequestMapping("/clearFtpFileInfo")
	public R clearFtpFileInfo() {
		sftpService.clearFtpFileInfo();
		return R.ok();
	}

	@ResponseBody
	@RequestMapping("/checkFtpFile")
	public R checkFtpFile() {
		return sftpService.checkFtpFile(sftpService.makeFtpDirectory(FileTypeEnum.DOWNLOAD));
	}
	
	@ResponseBody
	@RequestMapping("/checkFtpFileByDB")
	public R checkFtpFileByDB() {
		return sftpService.checkFtpFileByDB();
	}

	@ResponseBody
	@RequestMapping("/downloadFtpFileByFtpDirectory")
	public R downloadFtpFileByFtpDirectory(String ftpDirectory) {
		return sftpService.downloadFtpFileByFtpDirectory(ftpDirectory, ftpServerInfo.getLocalDirectory());
	}

	@ResponseBody
	@RequestMapping("/downloadFtpFileByDB")
	public R downloadFtpFileByDB() {
		return sftpService.downloadFtpFileByDB();
	}

	/*@ResponseBody
	@RequestMapping("/downloadFtpFileByFtpPath")
	public R downloadFtpFileByFtpPath(String ftpPath) {
		if (ftpPath != null && ftpPath != "") {
			return sftpService.downloadFtpFielByFtpPath(ftpPath, ftpServerInfo.getLocalDirectory());
		} else {
			return R.error("请输入正确的ftp路径");
		}
	}*/
	
	@ResponseBody
	@RequestMapping("/downloadFtpFileAndUploadByFtpPath")
	public R downloadFtpFileAndUploadByFtpPath(String ftpPath) {
		if (ftpPath != null && ftpPath != "") {
			R re = sftpService.downloadFtpFielByFtpPath(ftpPath, ftpServerInfo.getLocalDirectory());
			if(!R.checkOK(re)) {
				return re;
			}
			R r = ftpService.uploadFileByLocalPath(ftpService.makeFtpDirectory(FileTypeEnum.UPLOAD),
					ftpServerInfo.getLocalDirectory()+ftpPath);
			if(!R.checkOK(r)) {
				return r;
			}
		} else {
			return R.error("请输入正确的ftp路径");
		}
		return R.ok();
	}
	
	@ResponseBody
	@RequestMapping("/downloadFtpFileAndUploadByFtpDirectory")
	public R downloadFtpFileAndUploadByFtpDirectory(String ftpDirectory) {
		if (ftpDirectory != null && ftpDirectory != "") {
			R re = sftpService.downloadFtpFileByFtpDirectory(ftpDirectory, ftpServerInfo.getLocalDirectory());
			if(!R.checkOK(re)) {
				return re;
			}
			R r = ftpService.uploadFileByLocalDirectory(sftpService.makeFtpDirectory(FileTypeEnum.UPLOAD),
					sftpService.makeLocalFileDirectory());
			if(!R.checkOK(r)) {
				return r;
			}
		} else {
			return R.error("请输入正确的ftp路径");
		}
		return R.ok();
	}

	@ResponseBody
	@RequestMapping("/downloadFtpFileByDefinition")
	public R downloadFtpFileByDefinition() {
		if (ftpServerInfo != null && ftpServerInfo.getDefinitionFileName() != null
				&& ftpServerInfo.getDefinitionFileName().size() > 0) {
			return sftpService.downloadFtpFileByDefinition(sftpService.makeFtpPathByDifiniton(),
					ftpServerInfo.getLocalDirectory());
		} else {
			return R.error("配置文件未正确配置");
		}
	}

	@ResponseBody
	@RequestMapping("/downloadFtpFileByDBAndThread")
	public R downloadFtpFileByDBAndThread() {
		return sftpService.downloadFtpFileByDBAndThread(ftpServerInfo.getFtpDirectory(),
				ftpServerInfo.getLocalDirectory(), 3);
	}

	@ResponseBody
	@RequestMapping("/getFtpFileListByDirectory")
	public List<FtpFileSimpleInfo> getFtpFileDirectory(String ftpDirectory) {
		if (ftpDirectory != null && ftpDirectory != "") {
			return sftpService.readFTPFileDirectory(ftpDirectory);
		} else {
			return new ArrayList<FtpFileSimpleInfo>();
		}
	}

	/*@ResponseBody
	@RequestMapping("/uploadFileByLocalDirectory")
	public R uploadFileByLocalDirectory(String localFileDirectory) {
		return sftpService.uploadFileByLocalDirectory(sftpService.makeFtpDirectory(FileTypeEnum.UPLOAD),
				localFileDirectory);
	}*/

	/*@ResponseBody
	@RequestMapping("/uploadFileByResources")
	public R uploadFileByResources() {
		return sftpService.uploadFileByLocalDirectory(sftpService.makeFtpDirectory(FileTypeEnum.UPLOAD),
				sftpService.makeLocalFileDirectory());
	}*/

	/*@ResponseBody
	@RequestMapping("/uploadFile")
	public R uploadFile(String localFilPath) {
		return sftpService.uploadFileByLocalPath(sftpService.makeFtpDirectory(FileTypeEnum.UPLOAD),
				localFilPath);
	}*/

	/*@ResponseBody
	@RequestMapping("/uploadFileByDB")
	public R uploadFileByDB() {
		return sftpService.uploadFileByDB(sftpService.makeFtpDirectory(FileTypeEnum.UPLOAD),
				ftpServerInfo.getLocalDirectory(), sftpService.makeFtpDirectory(FileTypeEnum.DOWNLOAD));
	}*/

	@ResponseBody
	@RequestMapping("/saveDefinitionFileInfo")
	public R saveDefinitionFileInfo() {
		return sftpService.saveDefinitionFileInfo(sftpService.makeFtpPathByDifiniton());
	}
}
